﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Resto.CostControl.Core.Domain.Model;
using Resto.CostControl.Core.Facade.SiteModule.DataTransferObject;
using Resto.CostControl.Core.Cache;
using Resto.CostControl.Core.Enums;

namespace Resto.CostControl.Core.Facade.DishDeliveryModule.DataTransferObject
{
    /// <summary>
    /// 原材料采购单DTO
    /// </summary>
    [Serializable]
    public class DishDeliveryDTO
    {
        #region 构造函数

        public DishDeliveryDTO() { }

        internal DishDeliveryDTO(DishDelivery delivery)
        {
            this.ID = delivery.ID;
            this.Code = delivery.Code;
            this.Address = delivery.Address;
            this.Date = delivery.Date;
            this.SiteID = delivery.SiteID;
            this.State = (DishDeliveryState)Enum.Parse(typeof(DishDeliveryState),delivery.State);

            List<DishDeliveryItemDTO> list = new List<DishDeliveryItemDTO>();
            foreach (DishDeliveryItem item in delivery.Items)
                list.Add(new DishDeliveryItemDTO(item));

            this.Items = list.ToArray();

        }

        #endregion

        #region 基本属性

        public string ID { get; set; }

        /// <summary>
        /// 合同编号
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        public DishDeliveryState State { get; set; }

        /// <summary>
        /// 日期
        /// </summary>
        public DateTime Date { get; set; }

        /// <summary>
        /// 收货地址
        /// </summary>
        public string Address { get; set; }

        /// <summary>
        /// 项目点ID
        /// </summary>
        public string SiteID { get; set; }

        /// <summary>
        /// 项目点名称
        /// </summary>
        public string SiteName
        {
            get
            {
                SiteDTO siteDTO = SiteDTOCache.Get(this.SiteID);
                if (siteDTO != null)
                    return siteDTO.Name;

                return string.Empty;

            }
        }

        public DishDeliveryItemDTO[] Items { get; set; }


        #endregion

        #region 方法

        internal DishDelivery ToDishDelivery()
        {
            DishDelivery order = new DishDelivery();
            CopyToDishDelivery(order);
            return order;
        }

        internal void CopyToDishDelivery(DishDelivery delivery)
        {
            delivery.Code = this.Code;
            delivery.Address = this.Address;
            delivery.Date = this.Date;
            delivery.SiteID = this.SiteID;
            delivery.State = this.State.ToString();

            //清除要删除的项
            string[] oldIds = (from o in delivery.Items
                            select o.ID).ToArray();

            string[] newIds = (from o in this.Items
                            select o.ID).ToArray();

            List<string> delIds = (from o in oldIds
                                where !newIds.Contains(o)
                                select o).ToList();

            delIds.ForEach(x => delivery.RemoveItem(x));


            //修改和新增项
            foreach (DishDeliveryItemDTO itemDTO in Items)
            {
                DishDeliveryItem item;
                if (!string.IsNullOrWhiteSpace(itemDTO.ID))
                {
                    item = delivery.GetItem(itemDTO.ID);
                    if (item != null)
                        itemDTO.CopyToDishDeliveryItem(item);
                }
                else
                    delivery.AddItem(itemDTO.ToDishDeliveryItem());
            }
        }

        #endregion
    }
}
